<?php
	$ssFilter = $this->arrParam['ssFilter'];
	$paginator = $this->arrParam['paginator'];
	$action_link = $this->baseUrl($this->currentController . '/filter/type/order');
	$iconPath = $this->baseUrl(PUBLIC_PATH . '/img/icon');

	if($ssFilter['state'] == 1) {
		$lblDelete = 'Hiện';
		$deleteType = 0;
		$deleteMsg = 'hiện';
	} else {
		$lblDelete = 'Ẩn';
		$deleteType = 1;
		$deleteMsg = 'ẩn';
	}

	$lblFullname = $this->cmsLinkSort('Họ tên', 'ho_ten', $ssFilter, $this->baseUrl(PUBLIC_PATH . '/img/icon'), $action_link);
	$lblEmail = $this->cmsLinkSort('Email','email', $ssFilter, $this->baseUrl(PUBLIC_PATH . '/img/icon'), $action_link);
	$lblUserName = $this->cmsLinkSort('Username', 'username', $ssFilter, $this->baseUrl(PUBLIC_PATH . '/img/icon'), $action_link);
	$lblRegisterday = $this->cmsLinkSort('Ngày tạo', 'ngay_tao', $ssFilter, $this->baseUrl(PUBLIC_PATH . '/img/icon'), $action_link);
	$lblUpdatedDay = $this->cmsLinkSort('Ngày cập nhật', 'ngay_cap_nhat', $ssFilter, $this->baseUrl(PUBLIC_PATH . '/img/icon'), $action_link);

	$state = $this->formSelect('state', $ssFilter['state'], array('style' => 'width:130px; margin-right:5px;'), array(0 => 'Hiển thị', 1 => 'Không hiển thị'));
	$keywords = $this->formText('keywords', $ssFilter['keywords'], array('class' => 'span3', 'placeholder' => 'Tìm kiếm', 'style' => 'margin-right:5px;'));

	$linkSearch = $this->baseUrl($this->currentController . '/filter/type/search/key/1');
	$btnSearch = $this->formButton('search-now', 'Search', array('onclick' => "OnSubmitForm('" . $linkSearch . "')",'class' => 'btn', 'style' => 'margin-right:5px;'));

	$linkResetSearch = $this->baseUrl($this->currentController . '/filter/type/search/key/0');
	$btnResetSearch = $this->formButton('clear-keywords', 'Clear', array('onclick' => "OnSubmitForm('" . $linkResetSearch . "')", 'class' => 'btn'));

	$strSearch = $state . ' ' . $keywords. ' ' . $btnSearch . ' ' . $btnResetSearch;
?>

<script type="text/javascript">
	$(function(){
		var left_pos = ($(window).width() - 360) / 2;
		var msg = '<?php echo $deleteMsg; ?>';

		// add multiple select / deselect functionality
		$("#chkAll").click(function () {
			$('.case').attr('checked', this.checked);
          	var countCheckBox = checkCheckBox();

            if (countCheckBox == 0) {
        		$('#delall').hide();
      		} else {
      			$('#delall').show();
      		}
    	});

	    // if all checkbox are selected, check the selectall checkbox and viceversa
	    $(".case").click(function() {
	    	var countCheckBox = checkCheckBox();

	    	if (countCheckBox == 0) {
	    		$('#delall').hide();
	    	} else {
	    		$('#delall').show();
	    	}

	        if($(".case").length == $(".case:checked").length) {
	            $("#chkAll").attr("checked", "checked");
	        } else {
	            $("#chkAll").removeAttr("checked");
	        }
	    });

	    $(".btn-del").click(function(e) {
	    	e.preventDefault();
	        var location = $(this).attr('href');
	        bootbox.confirm("Bạn có chắc muốn " + msg + " dữ liệu này?", "Không", "Có", function(confirmed) {
	            if (confirmed) {
	            	window.location.replace(location);
	            }
	        });

	        $('.bootbox').attr('style', 'overflow: hidden; width: 360px; left: 0; margin-left: ' + left_pos + 'px; top: 0; margin-top: 200px;');
	    });

	    $("#delall").click(function(e) {
	    	e.preventDefault();
	        bootbox.confirm("Bạn có chắc muốn " + msg + " tất cả dữ liệu này?", "Không", "Có", function(confirmed) {
	            if(confirmed) {
	            	 document.appForm.action = '/admin/user/delete';
	                 document.appForm.submit();
	            }
	        });

	        $('.bootbox').attr('style', 'overflow: hidden; width: 360px; left: 0; margin-left: ' + left_pos + 'px; top: 0; margin-top: 200px;');
	    });
	});
</script>

<div id="toolbar-box" class="title">
	<div class="top-box">
		<div class="top-box">
			<div class="top-box"></div>
		</div>
	</div>
	<div class="m-box">
		<div class="icon-page title-users"></div>
		<div class="toolbar-button"></div>
		<div class="h2">Danh sách user</div>
		<div class="clear"></div>
	</div>
	<div class="bottom-box">
		<div class="bottom-box">
			<div class="bottom-box"></div>
		</div>
	</div>
</div>
<form
	name="appForm" action="" method="POST">
	<input type="hidden" id="delete-type" name="delete-type" value="<?php echo $deleteType; ?>" />
	<?php 
		if ($this->msg != '') {
			echo "<div class='alert alert-error' style='clear:both; margin-bottom: 5px;'>" . $this->msg . "</div>";
		}
	?>
	<!-- Search btn -->
	<p class="search-box input-append">
		<?php echo $strSearch; ?>
	</p>
	<div class="total-items">
		<!-- Total items label -->
		<span><?php echo $this->totalItems; ?> items</span>
		<!--  Delete all btn -->
		<a id="delall" style="display: none;" class="btn btn-danger btn-mini btn-delall" href="#"><i class="icon-remove icon-white"></i><?php echo $lblDelete; ?></a>
	</div>
	<!-- Main grid  -->
	<table class="gridview">
		<thead>
			<tr class="success">
				<th width="20" class="check-column"><input id="chkAll" type="checkbox" value="0" name="chkAll"></th>
				<th width="90">Hình đại diện</th>
				<th width="195"><?php echo $lblFullname; ?></th>
				<th width="195"><?php echo $lblEmail; ?></th>
				<th><?php echo $lblUserName; ?></th>
				<th width="130"><?php echo $lblRegisterday; ?></th>
				<th width="130"><?php echo $lblUpdatedDay; ?></th>
			</tr>
		</thead>
		<tbody>
		<?php 
			$modal = '';
			$dateTimeFormat = 'd-m-Y H:i:s';
			$dateFormat = 'd-m-Y';

			if(count($this->Items) > 0) {
				foreach($this->Items as $key => $val) {
					$id    		   	= $val->getId();
					$email    	 	= $val->getEmail();
					$fullname    	= $val->getHo_ten();
					$username		= $val->getUserName();
					$mainacc     	= number_format($val->getTai_khoan_chinh());
					$extacc      	= number_format($val->getTai_khoan_khuyen_mai());
					$point      	= number_format($val->getDiem());
					$sex			= $val->getGioi_tinh() == 0 ? "Nữ" : "Nam";
					$birthDay    	= date($dateFormat, strtotime($val->getNgay_sinh()));
					$address 	 	= $val->getDia_chi();
					$avatarImg   	= "<img class='avatar' src='/public/resources/user/user/avatars/" . $val->getHinh_anh() . "' width='60px' height='60px' onerror=\"this.onerror=null; this.src='/public/resources/user/user/avatars/no_avatar.png';\" />";
					$telephone   	= $val->getDien_thoai();
					$lastlogin  	= date($dateTimeFormat, strtotime($val->getDang_nhap_cuoi()));
					$active			= $val->getKich_hoat() == 0 ? "Chưa kích hoạt" : "Đã kích hoạt";
					$ngay_tao      	= date($dateTimeFormat, strtotime($val->getNgay_tao()));
					$ngay_cap_nhat 	= date($dateTimeFormat, strtotime($val->getNgay_cap_nhat()));
					$nguoi_cap_nhat = $val->getTen_nguoi_cap_nhat();
					$ngay_kich_hoat = $val->getNgay_kich_hoat() != null ? date($dateFormat, strtotime($val->getNgay_kich_hoat())) : '';
					$modalLink	   	= 'userModel' . $id;
					$linkEdit 	   	= $this->baseUrl($this->currentController . '/edit/id/' . $id . '/page/' . $paginator["currentPage"]);
					$linkDelete    	= $this->baseUrl($this->currentController . '/delete/delete-type/' . $deleteType . '/id/' . $id);
					$chkid   	   	= $this->formCheckbox('cid[]', $id, array('class' => 'case'));

					$action 	   	= '<div class="row-actions">
										<div class="btn-toolbar">
											<div class="btn-group">
	  							   				<a class="btn btn-info btn-mini" href="#' . $modalLink . '" role="button" data-toggle="modal"><i class="icon-user icon-white"></i>Xem</a>
		                           				<a class="btn btn-danger btn-mini btn-del" href="' . $linkDelete . '"><i class="icon-remove icon-white"></i>' . $lblDelete . '</a>
                               		  </div></div></div>';

					$detailinfo	= '';
					$detailinfo	.= '<table class="dialog_table table table-hover table-bordered">';
					$detailinfo .= '<tr><th class="span2">Username</th><td>'. $username . '</td><tr>';
					$detailinfo .= '<tr><th class="span2">Hình đại diện</th><td>' . $avatarImg . '</td><tr>';
					$detailinfo .= '<tr><th class="span2">Họ tên</th><td>' . $fullname . '</td><tr>';
					$detailinfo .= '<tr><th class="span2">Giới tinh</th><td>' . $sex . '</td><tr>';
					$detailinfo .= '<tr><th class="span2">Ngày sinh</th><td>' . $birthDay . '</td><tr>';
					$detailinfo .= '<tr><th class="span2">Địa chỉ</th><td>' . $address . '</td><tr>';
					$detailinfo .= '<tr><th class="span2">Điện thoại</th><td>' . $telephone . '</td><tr>';
					$detailinfo .= '<tr><th class="span2">Email</th><td>' . $email . '</td><tr>';
					$detailinfo .= '<tr><th class="span2">Tài khoản chính</th><td>' . $mainacc . ' VND</td><tr>';
					$detailinfo .= '<tr><th class="span2">Tài khoản KM</th><td>' . $extacc . ' VND</td><tr>';
					$detailinfo .= '<tr><th class="span2">Điểm</th><td>' . $point . '</td><tr>';
					$detailinfo .= '<tr><th class="span2">Tình trạng</th><td>' . $active . '</td><tr>';
					$detailinfo .= '<tr><th class="span2">Ngày kích hoạt</th><td>' . $ngay_kich_hoat . '</td><tr>';
					$detailinfo .= '<tr><th class="span2">Ngày đăng nhập</th><td>' . $lastlogin . '</td><tr>';
					$detailinfo .= '<tr><th class="span2">Ngày tạo</th><td>' . $ngay_tao . '</td><tr>';
					$detailinfo .= '<tr><th class="span2">Ngày cập nhật</th><td>' . $ngay_cap_nhat . '</td><tr>';
					$detailinfo .= '<tr><th class="span2">Người cập nhật</th><td>' . $nguoi_cap_nhat . '</td><tr>';
					$detailinfo    .= '</table>';

					$modal 		   .= '<div id="' . $modalLink . '" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  									  <div class="modal-header">
						    		  <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
						    		  <h4 id="myModalLabel">Thông tin chi tiết</h4></div>
						  			  <div class="modal-body">'. $detailinfo . '</div>
						  			  </div>';

					$detailinfo		= null;

					echo '<tr>';
					echo '<th class="check-column" scope="row">' . $chkid . '</th>';
					echo '<td class="first">' . $avatarImg . '</td>';
					echo '<td><strong><a href="#' . $modalLink . '" data-toggle="modal">' . Zendhlt_Encode::truncateString_($fullname, 31) . '</a></strong>' . $action . '</td>';
					echo '<td ><a href="mailto:' . $email . '">' . Zendhlt_Encode::truncateString_($email, 31) . '</a></td>';
					echo '<td>' . Zendhlt_Encode::truncateString_($username, 31) . '</td>';
					echo '<td>' . $ngay_tao . '</td>';
					echo '<td>' . $ngay_cap_nhat . '</td>';
					echo '</tr>';
				}
			} else {
		?>
				<tr><td colspan="6" align="center">Không có dữ liệu.</td></tr>
		<?php
			}
		?>
		</tbody>
		<!-- BEGIN: PAGINATION (FOOT) -->
		<tfoot>
			<tr>
				<td colspan="8">
					<div class="container">
						<div class="pagination">
							<?php
								$currentUrl = $this->baseUrl($this->currentController . '/index');
								echo $this->paginationControl($this->panigator, 'Sliding', 'pagination.phtml', array('currentUrl' => $currentUrl));
							?>
						</div>
					</div>
				</td>
			</tr>
		</tfoot>
		<!-- END: PAGINATION (FOOT) -->
	</table>
</form>

<?php echo $modal; ?>